package action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.ICity;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.CityService;
import service.ServiceManager;
import form.AdminCityForm;

public class AdminCityAction extends ConnectionAwareAction  {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		AdminCityForm input = (AdminCityForm) form;
		
		int[] id_city = input.getId_city();
		String[] name_city = input.getName_city();
	    	CityService cityService = ServiceManager.getDefault().getCityService(); 
	    	String add_name_city = input.getAdd_name_city();
			if (add_name_city !=""){ 
				if (request.getParameter("Add") != null){
				    cityService.setCity(add_name_city);
				    add_name_city = "";
				}
			}
			
			if (request.getParameter("Updat") != null){      
				if (id_city !=null){
					List<ICity> cityUp = cityService.getCityById(id_city); 
					request.setAttribute("cityUp", cityUp);
				}
			}
			
			if (request.getParameter("UpdateCity") != null){
				if(id_city !=null && name_city !=null){
					cityService.setCityUpdate(id_city,name_city);
					id_city = null;
					input.reset(id_city);
				}
			}
			if (request.getParameter("Delete") != null){
				if(id_city !=null){
					cityService.delete(id_city); 
					id_city = null;
					input.reset(id_city);
				}
			}
			List<ICity> citys = cityService.getCity();
	    	request.setAttribute("citys", citys);
	    	
		return mapping.findForward(ForwardActionTo.SUCCESS);
	}

}
